<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title></title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="../../layuiadmin/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="../../layuiadmin/style/admin.css" media="all">
</head>
<body>

<div class="layui-fluid">
    <div class="layui-card">

        <div class="layui-card-body">
            <div style="padding-bottom: 10px;">
                <button class="layui-btn layuiadmin-btn-admin" data-type="add">填写数据库信息</button>

                <!--<button class="layui-btn layuiadmin-btn-admin" data-type="gen" >生成代码</button>-->
                <form style="float: right" action="/genCode" method="post">
                    <input type="text" id="jsonData" name="json-data" hidden>
                    <input type="text" id="tables" name="tables" hidden>
                    <button type="submit" class="layui-btn layuiadmin-btn-admin" lay-submit="" lay-filter="gen-code">
                        生成代码
                    </button>
                </form>


            </div>

            <table id="LAY-user-back-manage" lay-filter="LAY-user-back-manage"></table>

        </div>
    </div>
</div>

<script src="../../layuiadmin/layui/layui.js"></script>
<script>
    layui.config({
        base: '../../../layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'table'], function () {
        var $ = layui.$
            , form = layui.form
            , table = layui.table
            , setter = layui.setter
            , admin = layui.admin;
        ;
        var dataSource = '';
        table.render({
            elem: '#LAY-user-back-manage'
            // , url: '/list' //模拟接口
            // , method: 'post'
            // , contentType: 'application/json'
            , data: []
            , page: true //开启分页
            , toolbar: true //让工具栏左侧显示默认的内置模板
            , totalRow: true
            , loading: true
            , cols: [[
                {type: 'checkbox', fixed: 'left'}
                // , {field: 'id', width: 100, title: 'ID', sort: true}
                , {field: 'name', title: '表名', minWidth: 100}
                , {field: 'engine', title: '引擎', minWidth: 100}
                , {field: 'comment', title: '表备注'}
                , {field: 'createTime', title: '创建时间'}

            ]]
            , parseData: function (res) { //res 即为原始返回的数据
                return {
                    "code": res.status, //解析接口状态
                    "msg": res.message, //解析提示文本
                    "count": res.object.total, //解析数据长度
                    "data": res.object.records //解析数据列表
                };
            }
            , response: {
                statusCode: 200 //规定成功的状态码，默认：0

            }
            , page: true
            , limit: 30
            // , height: 'full-220'
        });

        //监听提交
        form.on('submit(gen-code)', function (data) {
            var checkStatus = table.checkStatus('LAY-user-back-manage')
                , checkData = checkStatus.data; //得到选中的数据
            if (checkData.length === 0) {

                layer.msg('请选择需要生成代码的表');
                return false;
            }
            var tables = [];
            for (var i = 0; i < checkData.length; i++) {
                tables.push(checkData[i].name);
            }
            var jsonData = setter.getSetting();
            for (x in dataSource) {
                jsonData[x] = dataSource[x];
            }
            jsonData['strategy.include'] = tables;
            $("#jsonData").val(JSON.stringify(jsonData));
            $("#tables").val(tables);

            return true;
        });
        //事件
        var active = {
            add: function () {
                layer.open({
                    type: 2
                    , title: '添加数据库配置信息'
                    , content: 'datasourceform.html'
                    , area: ['420px', '420px']
                    , btn: ['确定', '取消']
                    , scrollbar: false
                    , success: function (layero, index) {

                    }
                    , yes: function (index, layero) {
                        var iframeWindow = window['layui-layer-iframe' + index]
                            , submit = layero.find('iframe').contents().find('#LAY-data-source-submit');

                        //监听提交
                        iframeWindow.layui.form.on('submit(LAY-data-source-submit)', function (data) {
                            dataSource = data.field; //获取提交的字段
                            //提交 Ajax 成功后，静态更新表格中的数据
                            //$.ajax({});
                            admin.req({
                                url: '/list'
                                , type: "POST"
                                , contentType: "application/json"
                                , data: JSON.stringify(dataSource)
                                , success: function (result) {
                                    if (result.status === 200) {

                                        //执行重载
                                        table.reload('LAY-user-back-manage', {
                                            data: result.object
                                        });
                                    }

                                }
                            });

                            layer.close(index); //关闭弹层
                        });

                        submit.trigger('click');
                    }
                });
            }
        }
        $('.layui-btn.layuiadmin-btn-admin').on('click', function () {
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });
    });
</script>
</body>
</html>

